#include "SkillRelive.h"
#include "Character/SkillPart.h"
#include "Character/MovePart.h"
#include "Character/CreatureMgr.h"
#include "Character/monster/Monster.h"
SkillRelive::SkillRelive()
{

}

void SkillRelive::OnDamageTimedo(uint32_t skillId)
{
	cList  targetCidList;
	cList::iterator iter;
	Monster * pMonster = nullptr;
	Creature * pCreature = nullptr;
	MonsCreateParam createParam;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->FindCreatures(pSkill->_pSkillmeta, m_masterPos, m_skillDir, 0, m_targetPos, m_curDamageCount, m_focoTime, targetCidList);
	iter = targetCidList.begin();
	//把所有人拉到一起
	for (; iter != targetCidList.end(); ++iter)
	{
		pCreature = g_GetCreatureMgr()->GetCreature(*iter);
		if (pCreature)
		{
			//if (pCreature->CreatureKind() == CREATURE_PLAYER)
			//{
			//	Player * pPlayer = dynamic_cast<Player*>(pCreature);
			//	if (pPlayer)
			//	{
			//		pPlayer->OnRevive(m_pPart->GetMasterCid());
			//	}
			//}
			//else 
			if (pCreature->GetCid()!=*iter &&  pCreature->CreatureKind() == CREATURE_MONSTER && pCreature->IsDead())
			{
				pMonster = dynamic_cast<Monster*>(pCreature);
				if (pMonster)
				{
					pMonster->OnDestroyMonsterNow();
					createParam.clear();
					createParam.createType = EMonsCreateType_ReliveSkill;
					createParam.createTypeVal = skillId;
					g_GetCreatureMgr()->CreateMonster(pMonster->GetMonsterInfo()->monsterID, pMonster->GetSceneId(), pMonster->GetPos(), createParam);
					//pMonster->OnRevive(m_pPart->GetMasterCid());
				}
			}
		}
	}

	m_pPart->SkillResult(skillId, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, targetCidList);

	//技能一次释放完成，需要删通知删除
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
}

void SkillRelive::OnDamageTimedoPlayer(uint32_t skillId, cList & vtargetList)
{
	cList::iterator iter;
	Creature * pCreature = nullptr;
	Player * pPlayer = nullptr;
	Monster * pMonster = nullptr;
	int64_t hp = 0; 
	int64_t mp = 0;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);
	MMOLOG_PROCESS_ERROR(pSkill->_pSkillmeta->funcAttackValue.size() >= 2);
	m_pPart->KillTimedo(m_nGlobelID);

	if (m_curDamageCount<=0)
	{
		iter = vtargetList.begin();
		//把所有人拉到一起
		for (; iter != vtargetList.end(); ++iter)
		{
			pCreature = g_GetCreatureMgr()->GetCreature(*iter);
			if (pCreature)
			{
				if (!pCreature->IsDead())
				{
					continue;
				}
				//先计算加的血和蓝
				hp = m_pPart->GetSkillAttackfix(skillId, 0, pCreature);
				mp = m_pPart->GetSkillAttackfix(skillId, 1, pCreature);

				if (pCreature->CreatureKind() == CREATURE_PLAYER)
				{
					pPlayer = dynamic_cast<Player*>(pCreature);
					if (pPlayer)
					{
						pPlayer->OnRevive(m_pPart->GetMasterCid(), hp, mp);
					}
				}
				else if (pCreature->CreatureKind() == CREATURE_MONSTER)
				{
					 pMonster = dynamic_cast<Monster*>(pCreature);
					if (pMonster)
					{
						pMonster->OnRevive(pMonster->GetCid());
						//先这样，这里要测试再调
						//pMonster->SetDestory();
						//g_GetCreatureMgr()->WaitDestory(pMonster->GetCid());
						//g_GetCreatureMgr()->CreateMonster(pMonster->GetMonsterInfo()->monsterID, pMonster->GetSceneId(), pMonster->GetBornPos());
					}
				}
			}
		}
	}
	
	//计算技能结果
	m_pPart->SkillResult(skillId, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, vtargetList);

	if ((uint32_t)pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{
		//技能一次释放完成，需要删通知删除
		m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
}